package com.ljf.authority.api.service.impl;

import com.ljf.authority.domain.repository.AuthorityBaseApiTabulationRepository;
import com.ljf.authority.domain.service.AuthorityBaseApiTabulationDomainService;
import com.ljf.authority.domain.validator.AuthorityBaseApiTabulationValidator;
import com.ljf.authority.dto.command.AuthorityBaseApiTabulationActivationCommand;
import com.ljf.authority.dto.command.AuthorityBaseApiTabulationCommand;
import com.ljf.authority.dto.command.AuthorityBaseApiTabulationMenuCommand;
import com.ljf.authority.dto.command.AuthorityBaseApiTabulationTemplateCommand;
import com.ljf.authority.dto.query.AuthorityBaseApiTabulationQuery;
import com.ljf.authority.model.AuthorityBaseApiTabulation;
import com.ljf.authority.service.IAuthorityBaseApiTabulationService;
import com.ljf.authority.vo.AuthorityBaseApiTabulationInfoVO;
import com.ljf.authority.vo.AuthorityBaseApiTabulationMenuVO;
import com.ljf.authority.vo.AuthorityBaseApiTabulationVO;
import com.ljf.common.api.service.impl.BasisServiceImpl;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.result.ServiceResultModel;
import com.ljf.common.utils.ServiceResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * API列表定义  服务接口实现
 *
 * @author LJF
 * @since 2024-10-25
 */
@Service
public class AuthorityBaseApiTabulationServiceImpl extends BasisServiceImpl<AuthorityBaseApiTabulationDomainService, AuthorityBaseApiTabulationValidator, AuthorityBaseApiTabulationRepository, AuthorityBaseApiTabulationQuery, AuthorityBaseApiTabulationVO, AuthorityBaseApiTabulation, AuthorityBaseApiTabulationCommand> implements IAuthorityBaseApiTabulationService {

    /**
     * 校验接口权限
     *
     * @param user
     * @param url
     * @param method
     * @return
     * @throws ServiceException
     */
    @Override
    public boolean checkAuthorityApi(String user, String url, Integer method) throws ServiceException {
        return domainService.checkAuthorityApi(user, url, method);
    }

    /**
     * 通过模板批量新增API列表定义
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel addBatchByTemplate(AuthorityBaseApiTabulationTemplateCommand command) throws ServiceException {
        validator.checkAddBatchByTemplate(command);
        domainService.addBatchByTemplate(command);
        return ServiceResultUtil.success();
    }

    /**
     * 修改API列表定义激活状态
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel editActivation(AuthorityBaseApiTabulationActivationCommand command) throws ServiceException {
        validator.checkEditActivation(command);
        domainService.editActivation(command);
        return ServiceResultUtil.success();
    }

    /**
     * 修改API列表定义关联菜单
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel editMenu(AuthorityBaseApiTabulationMenuCommand command) throws ServiceException {
        validator.checkEditMenu(command);
        domainService.editMenu(command);
        return ServiceResultUtil.success();
    }

    /**
     * 查询API列表表单信息
     *
     * @param url
     * @param method
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<AuthorityBaseApiTabulationInfoVO> queryInfo(String url, Integer method) throws ServiceException {
        validator.checkQueryInfo(url, method);
        AuthorityBaseApiTabulationInfoVO result = domainService.queryInfo(url, method);
        List<AuthorityBaseApiTabulationInfoVO> resultList = new ArrayList<>();
        resultList.add(result);
        return ServiceResultUtil.success(resultList);
    }

    /**
     * 查询API列表模板
     *
     * @param tableName
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<AuthorityBaseApiTabulationInfoVO> queryTemplate(String tableName) throws ServiceException {
        validator.checkQueryTemplate(tableName);
        List<AuthorityBaseApiTabulationInfoVO> resultList = domainService.queryTemplate(tableName);
        return ServiceResultUtil.success(resultList);
    }

    /**
     * 根据ID查询API列表关联菜单
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<AuthorityBaseApiTabulationMenuVO> queryMenuById(Long id) throws ServiceException {
        validator.checkQueryMenuById(id);
        AuthorityBaseApiTabulationMenuVO result = domainService.queryMenuById(id);
        List<AuthorityBaseApiTabulationMenuVO> resultList = new ArrayList<>();
        resultList.add(result);
        return ServiceResultUtil.success(resultList);
    }

}
